# conding=utf-8

import requests
from lxml import etree

HREADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
    "Referer": "https://c.v4dwkcv.com/"
}

base_url = "https://www.dydytt.net"


# url = "https://www.dydytt.net/html/gndy/dyzz/index.html"


def parse_url(url):
    response = requests.get(url, headers=HREADERS)
    res = response.text
    #  res = response.content.decode("gbk")
    # print(res)
    html = etree.HTML(res)
    href_list = html.xpath("//table[@class='tbspan']//a/@href")
    href_list = map(lambda href: base_url + href, href_list)
    return href_list


def do_parse_details(href):
    movice = {}

    response = requests.get(href, headers=HREADERS)
    response.encoding = 'gbk'
    res = response.text
    # res = response.content.decode("gbk")
    html = etree.HTML(res)
    name = html.xpath("//div[@class='title_all']//font[@color='#07519a']//text()")
    movice['name'] = name[0]
    zoom_e = html.xpath("//div[@id='Zoom']")[0]
    img = zoom_e.xpath(".//img/@src")
    movice['cover'] = img[0]
    contexts = zoom_e.xpath(".//text()")
    re_con = lambda c, re: c.replace(re, "").strip()
    for co in contexts:
        if co.startswith('◎年　　代'):
            year = re_con(co, '◎年　　代')
            movice['year'] = year
        if co.startswith('◎产　　地'):
            country = re_con(co, '◎产　　地')
            movice['country'] = country
        if co.startswith('◎豆瓣评分'):
            movice['doubanScore'] = re_con(co, '◎豆瓣评分')
    down_url = zoom_e.xpath(".//a/@href")
    movice['downUrl'] = down_url[0]
    print(movice)


def get_parse_all_url():
    base_url = "https://www.dydytt.net/html/gndy/dyzz/list_23_{}.html"
    for ind in range(1, 8):
        url = base_url.format(ind)
        print("解析--" + url)
        href_list = parse_url(url)
        for href in href_list:
            do_parse_details(href)



if __name__ == '__main__':
    get_parse_all_url()
